﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.customer.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.exception.RepeatRecordException;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.dao.*;
import com.hh.bss.customer.entity.*;
import com.hh.bss.customer.service.*;
import com.hh.bss.customer.vo.query.*;
import com.hh.bss.util.*;




@Service
@Transactional
public class CustomerServiceImpl extends BaseBusinessService<Customer,java.lang.Integer>  implements CustomerService<Customer,java.lang.Integer>{

	private CustomerDao customerDao;
	private CompanyDao companyDao;
	private CompanyService companyService;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setCustomerDaoImpl(CustomerDao dao) {
		this.customerDao = dao;
	}
	public void setCompanyServiceImpl(CompanyService service) {
		this.companyService = service;
	}
	public void setCompanyDaoImpl(CompanyDao dao) {
		this.companyDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.customerDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(CustomerQuery query) {
		return customerDao.findPage(query);
	}
	public void update(Company company) {
		boolean isRepeat=companyService.isCompanyRepeat(company);
		if (isRepeat) {			
			if(!StringHelper.isEmpty(company.getFullName()))
				throw new RepeatRecordException("公司["+company.getName()+"/"+company.getFullName()+"] 已经存在");	
			else throw new RepeatRecordException("公司"+company.getName()+"已经存在");
		}
		companyDao.update(company);
	}
	public void addComapnyCustomer(Company company, Customer customer) {
		// TODO Auto-generated method stub
		//保存company
		//判断公司是否重复
		boolean isRepeat=companyService.isCompanyRepeat(company);
		if (isRepeat) {			
			if(!StringHelper.isEmpty(company.getFullName()))
				throw new RepeatRecordException("公司["+company.getName()+"/"+company.getFullName()+"] 已经存在");	
			else throw new RepeatRecordException("公司"+company.getName()+"已经存在");
		}
		companyDao.save(company);
		companyDao.flush();
		//保存customer
		customer.setCompanyId(company.getId());
		customerDao.save(customer);
		
	}
	
	public Page findCustomerPage(CustomerQuery query) {
		// TODO Auto-generated method stub
		return customerDao.findCustomerPage(query);
	}
	public Page findBdCustomerPage(CustomerQuery query) {
		// TODO Auto-generated method stub
		return customerDao.findBdCustomerPage(query);
	}
	
	public List getCustomerContacter(Integer custId, String type) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer();
		List values = new ArrayList();
		sql.append(" select * from ( ");
		sql.append(" select d.cv_name cvName,d.cv_name_en cvNameEn,d.email from customer b,customer_contacter c ");
		sql.append(" left join  cv d on c.cv_id=d.id ");
		//sql.append(" left join (select cv_id,group_concat((nbr) separator ',') as email from cv_contact_detail where contact_type='").append(ConstantDic.ContactType_EMAIL).append("' group by cv_id) e on c.cv_id=e.cv_id ");
		sql.append(" where b.id=? and c.cust_id=b.id and c.role=? ");		
		sql.append(" ) t ");
		values.add(custId);
		values.add(type);
		
		return customerDao.getJdbcDao().findByQuery(sql.toString(),values.toArray(), new MapObjectRowMapper());
		
	}
	
	public Map getCustomerJobInfo(Integer custId) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer();
		List values = new ArrayList();
		sql.append(" select * from ( ");
		sql.append(" select a.abbr_name name,a.industry,a.location from company a, customer b where a.id=b.company_id  and b.id=? ");			
		sql.append(" ) t ");
		values.add(custId);
		
		
		List l= customerDao.getJdbcDao().findByQuery(sql.toString(),values.toArray(), new MapObjectRowMapper());
		Map m=new HashMap();
		if (l!=null&&l.size()>0) m=(Map)l.get(0);
		
		sql = new StringBuffer();
		values = new ArrayList();

		sql.append(" select * from ( ");
		sql.append(" select b.cv_name cvName,a.cv_id cvId,a.role from customer_contacter a, cv b where  a.cust_id=? and a.cv_id=b.id  ");			
		sql.append(" ) t ");
		values.add(custId);
		l= customerDao.getJdbcDao().findByQuery(sql.toString(),values.toArray(), new MapObjectRowMapper());		
		
		for(int i=0;l!=null&&i<l.size();i++){
			Map mc =(Map)l.get(i);
			//职位联系人
			if (ConstantDic.CUSTOMER_CONTACTER_TYPE_JOB_CONTACTER.equals((String)mc.get("role"))){
		           m.put("jobContacter", mc.get("cvName"));
		           m.put("jobContacterId", mc.get("cvId"));
			}
			
			//财务联系人
			if (ConstantDic.CUSTOMER_CONTACTER_TYPE_FINANCE_CONTACTER.equals((String)mc.get("role"))){
		           m.put("financeContacter", mc.get("cvName"));
		           m.put("financeContacterId", mc.get("cvId"));
			}
            
			
		}
		
		
		return m;
	}	
	public Map getSimpleCompanyInfo(Integer custId) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer();
		List values = new ArrayList();
		sql.append(" select * from ( ");
		sql.append(" select a.abbr_name custName,a.name chName,a.full_name enName from company a, customer b where a.id=b.company_id  and b.id=? ");			
		sql.append(" ) t ");
		values.add(custId);
		
		
		List l= customerDao.getJdbcDao().findByQuery(sql.toString(),values.toArray(), new MapObjectRowMapper());
		Map m=new HashMap();
		if (l!=null&&l.size()>0) m=(Map)l.get(0);
		
		return m;
	}
}
